package concurrent;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

public class ArrayRemainder {

    public static void main(String[] args) {
        int[] arr = new int[301];

        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        int[] remainders = new int[arr.length];

        int threadCount = 9;

        CountDownLatch downLatch = new CountDownLatch(threadCount);
        int number = 8;
        int eachThreadElements = (int) Math.ceil(1.0 * arr.length / threadCount);
        for (int i = 0; i < threadCount; i++) {

            int start = i * eachThreadElements;
            int end = start + Math.min(arr.length - start, eachThreadElements);

            new Thread(() -> {
                for (int j = start; j < end; j++) {
                    remainders[j] = arr[j] % number;
                }
                downLatch.countDown();
            }).start();
        }
        try {
            downLatch.await();
        } catch (InterruptedException ignore) {
            Thread.currentThread().interrupt();
        }
        System.out.println(Arrays.toString(remainders));
    }

}
